- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 1   F u l l   c o d e   o f   t h e   F a c t   S a l e   q u e r y 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 l e t 
 
         S o u r c e   =   S q l . D a t a b a s e s ( " l o c a l h o s t " ,   [ C r e a t e N a v i g a t i o n P r o p e r t i e s = f a l s e ] ) , 
 
         W i d e W o r l d I m p o r t e r s D W   =   S o u r c e { [ N a m e = " W i d e W o r l d I m p o r t e r s D W " ] } [ D a t a ] , 
 
         F a c t _ S a l e   =   W i d e W o r l d I m p o r t e r s D W { [ S c h e m a = " F a c t " , I t e m = " S a l e " ] } [ D a t a ] , 
 
         # " R e m o v e d   O t h e r   C o l u m n s "   =   T a b l e . S e l e c t C o l u m n s ( F a c t _ S a l e , { " C i t y   K e y " ,   " C u s t o m e r   K e y " ,   " B i l l   T o   C u s t o m e r   K e y " ,   " S t o c k   I t e m   K e y " ,   " I n v o i c e   D a t e   K e y " ,   " D e l i v e r y   D a t e   K e y " ,   " S a l e s p e r s o n   K e y " ,   " W W I   I n v o i c e   I D " ,   " Q u a n t i t y " ,   " U n i t   P r i c e " ,   " T a x   R a t e " ,   " T o t a l   E x c l u d i n g   T a x " ,   " T a x   A m o u n t " ,   " P r o f i t " ,   " T o t a l   I n c l u d i n g   T a x " } ) 
 
 i n 
 
         # " R e m o v e d   O t h e r   C o l u m n s " 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 2   S a l e I n i t i a l   a n d   F a c t   S a l e   q u e r i e s 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 / /   S a l e I n i t i a l 
 
 l e t 
 
         S o u r c e   =   S q l . D a t a b a s e s ( " l o c a l h o s t " ,   [ C r e a t e N a v i g a t i o n P r o p e r t i e s = f a l s e ] ) , 
 
         W i d e W o r l d I m p o r t e r s D W   =   S o u r c e { [ N a m e = " W i d e W o r l d I m p o r t e r s D W " ] } [ D a t a ] , 
 
         F a c t _ S a l e   =   W i d e W o r l d I m p o r t e r s D W { [ S c h e m a = " F a c t " , I t e m = " S a l e " ] } [ D a t a ] 
 
 i n 
 
         F a c t _ S a l e 
 
 
 
 / /   F a c t   S a l e 
 
 l e t 
 
         S o u r c e   =   S a l e I n i t i a l , 
 
         # " R e m o v e d   O t h e r   C o l u m n s "   =   T a b l e . S e l e c t C o l u m n s ( S o u r c e , { " C i t y   K e y " ,   " C u s t o m e r   K e y " ,   " B i l l   T o   C u s t o m e r   K e y " ,   " S t o c k   I t e m   K e y " ,   " I n v o i c e   D a t e   K e y " ,   " D e l i v e r y   D a t e   K e y " ,   " S a l e s p e r s o n   K e y " ,   " W W I   I n v o i c e   I D " ,   " Q u a n t i t y " ,   " U n i t   P r i c e " ,   " T a x   R a t e " ,   " T o t a l   E x c l u d i n g   T a x " ,   " T a x   A m o u n t " ,   " P r o f i t " ,   " T o t a l   I n c l u d i n g   T a x " } ) 
 
 i n 
 
         # " R e m o v e d   O t h e r   C o l u m n s " 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 3   N a t i v e   q u e r y   o f   t h e   R e m o v e d   O t h e r   C o l u m n s   s t e p 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 s e l e c t   [ C i t y   K e y ] , 
 
         [ C u s t o m e r   K e y ] , 
 
         [ B i l l   T o   C u s t o m e r   K e y ] , 
 
         [ S t o c k   I t e m   K e y ] , 
 
         [ I n v o i c e   D a t e   K e y ] , 
 
         [ D e l i v e r y   D a t e   K e y ] , 
 
         [ S a l e s p e r s o n   K e y ] , 
 
         [ W W I   I n v o i c e   I D ] , 
 
         [ Q u a n t i t y ] , 
 
         [ U n i t   P r i c e ] , 
 
         [ T a x   R a t e ] , 
 
         [ T o t a l   E x c l u d i n g   T a x ] , 
 
         [ T a x   A m o u n t ] , 
 
         [ P r o f i t ] , 
 
         [ T o t a l   I n c l u d i n g   T a x ] 
 
 f r o m   [ F a c t ] . [ S a l e ]   a s   [ $ T a b l e ] 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 4  A   c o m m e n t   n e x t   t o   t h e   R e m o v e d   O t h e r   C o l u m n s   s t e p 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 l e t 
 
         S o u r c e   =   S a l e I n i t i a l , 
 
         / /   L e s s   i s   m o r e 
 
         # " R e m o v e d   O t h e r   C o l u m n s "   =   T a b l e . S e l e c t C o l u m n s ( S o u r c e , { " C i t y   K e y " ,   " C u s t o m e r   K e y " ,   " B i l l   T o   C u s t o m e r   K e y " ,   " S t o c k   I t e m   K e y " ,   " I n v o i c e   D a t e   K e y " ,   " D e l i v e r y   D a t e   K e y " ,   " S a l e s p e r s o n   K e y " ,   " W W I   I n v o i c e   I D " ,   " Q u a n t i t y " ,   " U n i t   P r i c e " ,   " T a x   R a t e " ,   " T o t a l   E x c l u d i n g   T a x " ,   " T a x   A m o u n t " ,   " P r o f i t " ,   " T o t a l   I n c l u d i n g   T a x " } ) 
 
 i n 
 
         # " R e m o v e d   O t h e r   C o l u m n s " 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 5  M   q u e r y   a f t e r   r e m o v i n g   u n n e c e s s a r y   r o w s 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 l e t 
 
         S o u r c e   =   C s v . D o c u m e n t ( F i l e . C o n t e n t s ( " C : \ C o m p a n i o n \ T a r g e t . t x t " ) , [ D e l i m i t e r = " ; " ,   C o l u m n s = 3 ,   E n c o d i n g = 1 2 5 2 ,   Q u o t e S t y l e = Q u o t e S t y l e . N o n e ] ) , 
 
         # " P r o m o t e d   H e a d e r s "   =   T a b l e . P r o m o t e H e a d e r s ( S o u r c e ,   [ P r o m o t e A l l S c a l a r s = t r u e ] ) , 
 
         # " C h a n g e d   T y p e "   =   T a b l e . T r a n s f o r m C o l u m n T y p e s ( # " P r o m o t e d   H e a d e r s " , { { " C a l e n d a r Y e a r " ,   I n t 6 4 . T y p e } } ) , 
 
         # " K e p t   R a n g e   o f   R o w s "   =   T a b l e . R a n g e ( # " C h a n g e d   T y p e " , 3 , 9 ) 
 
 i n 
 
         # " K e p t   R a n g e   o f   R o w s " 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 6  T h e   c o m p l e t e   T a r g e t   q u e r y   s c r i p t 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 l e t 
 
         S o u r c e   =   C s v . D o c u m e n t ( F i l e . C o n t e n t s ( " C : \ C o m p a n i o n \ T a r g e t . t x t " ) , [ D e l i m i t e r = " ; " ,   C o l u m n s = 3 ,   E n c o d i n g = 1 2 5 2 ,   Q u o t e S t y l e = Q u o t e S t y l e . N o n e ] ) , 
 
         # " P r o m o t e d   H e a d e r s "   =   T a b l e . P r o m o t e H e a d e r s ( S o u r c e ,   [ P r o m o t e A l l S c a l a r s = t r u e ] ) , 
 
         # " C h a n g e d   T y p e "   =   T a b l e . T r a n s f o r m C o l u m n T y p e s ( # " P r o m o t e d   H e a d e r s " , { { " C a l e n d a r Y e a r " ,   I n t 6 4 . T y p e } } ) , 
 
         # " K e p t   R a n g e   o f   R o w s "   =   T a b l e . R a n g e ( # " C h a n g e d   T y p e " , 3 , 9 ) , 
 
         # " R e n a m e d   C o l u m n s "   =   T a b l e . R e n a m e C o l u m n s ( # " K e p t   R a n g e   o f   R o w s " , { { " C a l e n d a r Y e a r " ,   " C a l e n d a r   Y e a r " } } ) , 
 
         # " S p l i t   C o l u m n   b y   D e l i m i t e r "   =   T a b l e . S p l i t C o l u m n ( # " R e n a m e d   C o l u m n s " ,   " B i l l T o   T a r g e t " ,   S p l i t t e r . S p l i t T e x t B y E a c h D e l i m i t e r ( { "   " } ,   Q u o t e S t y l e . C s v ,   t r u e ) ,   { " B i l l T o   T a r g e t . 1 " ,   " B i l l T o   T a r g e t . 2 " } ) , 
 
         # " C h a n g e d   T y p e 1 "   =   T a b l e . T r a n s f o r m C o l u m n T y p e s ( # " S p l i t   C o l u m n   b y   D e l i m i t e r " , { { " B i l l T o   T a r g e t . 1 " ,   t y p e   t e x t } ,   { " B i l l T o   T a r g e t . 2 " ,   t y p e   t e x t } ,   { " O f f i c e " ,   t y p e   t e x t } } ) , 
 
         # " R e p l a c e d   V a l u e "   =   T a b l e . R e p l a c e V a l u e ( # " C h a n g e d   T y p e 1 " , " " , n u l l , R e p l a c e r . R e p l a c e V a l u e , { " O f f i c e " } ) , 
 
         # " C a p i t a l i z e d   E a c h   W o r d "   =   T a b l e . T r a n s f o r m C o l u m n s ( # " R e p l a c e d   V a l u e " , { { " O f f i c e " ,   T e x t . P r o p e r ,   t y p e   t e x t } } ) , 
 
         # " A d d e d   P r e f i x "   =   T a b l e . T r a n s f o r m C o l u m n s ( # " C a p i t a l i z e d   E a c h   W o r d " ,   { { " O f f i c e " ,   e a c h   " ( "   &   _ ,   t y p e   t e x t } } ) , 
 
         # " A d d e d   S u f f i x "   =   T a b l e . T r a n s f o r m C o l u m n s ( # " A d d e d   P r e f i x " ,   { { " O f f i c e " ,   e a c h   _   &   " ) " ,   t y p e   t e x t } } ) , 
 
         # " M e r g e d   C o l u m n s "   =   T a b l e . C o m b i n e C o l u m n s ( # " A d d e d   S u f f i x " , { " B i l l T o   T a r g e t . 1 " ,   " O f f i c e " } , C o m b i n e r . C o m b i n e T e x t B y D e l i m i t e r ( "   " ,   Q u o t e S t y l e . N o n e ) , " B i l l   T o   C u s t o m e r " ) , 
 
         # " R e n a m e d   C o l u m n s 1 "   =   T a b l e . R e n a m e C o l u m n s ( # " M e r g e d   C o l u m n s " , { { " B i l l T o   T a r g e t . 2 " ,   " T a r g e t " } } ) , 
 
         # " R e p l a c e d   V a l u e 1 "   =   T a b l e . R e p l a c e V a l u e ( # " R e n a m e d   C o l u m n s 1 " , " * " , " " , R e p l a c e r . R e p l a c e T e x t , { " T a r g e t " } ) , 
 
         # " C h a n g e d   T y p e 2 "   =   T a b l e . T r a n s f o r m C o l u m n T y p e s ( # " R e p l a c e d   V a l u e 1 " , { { " T a r g e t " ,   I n t 6 4 . T y p e } } ) , 
 
         # " M u l t i p l i e d   C o l u m n "   =   T a b l e . T r a n s f o r m C o l u m n s ( # " C h a n g e d   T y p e 2 " ,   { { " T a r g e t " ,   e a c h   _   *   1 0 0 0 0 0 0 ,   t y p e   n u m b e r } } ) , 
 
         # " R e o r d e r e d   C o l u m n s "   =   T a b l e . R e o r d e r C o l u m n s ( # " M u l t i p l i e d   C o l u m n " , { " C a l e n d a r   Y e a r " ,   " B i l l   T o   C u s t o m e r " ,   " T a r g e t " } ) , 
 
         # " S o r t e d   R o w s "   =   T a b l e . S o r t ( # " R e o r d e r e d   C o l u m n s " , { { " B i l l   T o   C u s t o m e r " ,   O r d e r . D e s c e n d i n g } ,   { " C a l e n d a r   Y e a r " ,   O r d e r . A s c e n d i n g } } ) 
 
 i n 
 
         # " S o r t e d   R o w s " 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 7  M   q u e r y   o f   T a r g e t 2 0 1 5 2 0 1 6 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 l e t 
 
         S o u r c e   =   E x c e l . W o r k b o o k ( F i l e . C o n t e n t s ( " C : \ C o m p a n i o n \ T a r g e t 2 0 1 5 2 0 1 6 . x l s x " ) ,   n u l l ,   t r u e ) , 
 
         S h e e t 1 _ S h e e t   =   S o u r c e { [ I t e m = " S h e e t 1 " , K i n d = " S h e e t " ] } [ D a t a ] , 
 
         # " C h a n g e d   T y p e "   =   T a b l e . T r a n s f o r m C o l u m n T y p e s ( S h e e t 1 _ S h e e t , { { " C o l u m n 1 " ,   t y p e   t e x t } ,   { " C o l u m n 2 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 3 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 4 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 5 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 6 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 7 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 8 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 9 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 1 0 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 1 1 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 1 2 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 1 3 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 1 4 " ,   t y p e   a n y } ,   { " C o l u m n 1 5 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 1 6 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 1 7 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 1 8 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 1 9 " ,   I n t 6 4 . T y p e } ,   { " C o l u m n 2 0 " ,   t y p e   a n y } ,   { " C o l u m n 2 1 " ,   t y p e   a n y } } ) , 
 
         # " T r a n s p o s e d   T a b l e "   =   T a b l e . T r a n s p o s e ( # " C h a n g e d   T y p e " ) , 
 
         # " F i l l e d   D o w n "   =   T a b l e . F i l l D o w n ( # " T r a n s p o s e d   T a b l e " , { " C o l u m n 1 " } ) , 
 
         # " F i l t e r e d   R o w s "   =   T a b l e . S e l e c t R o w s ( # " F i l l e d   D o w n " ,   e a c h   ( [ C o l u m n 1 ]   =   n u l l   o r   [ C o l u m n 1 ]   =   2 0 1 5   o r   [ C o l u m n 1 ]   =   2 0 1 6 ) ) , 
 
         # " P r o m o t e d   H e a d e r s "   =   T a b l e . P r o m o t e H e a d e r s ( # " F i l t e r e d   R o w s " ,   [ P r o m o t e A l l S c a l a r s = t r u e ] ) , 
 
         # " C h a n g e d   T y p e 1 "   =   T a b l e . T r a n s f o r m C o l u m n T y p e s ( # " P r o m o t e d   H e a d e r s " , { { " C o l u m n 1 " ,   I n t 6 4 . T y p e } ,   { " R o w   L a b e l s " ,   I n t 6 4 . T y p e } ,   { " N / A " ,   I n t 6 4 . T y p e } ,   { " T a i l s p i n   T o y s   ( H e a d   O f f i c e ) " ,   I n t 6 4 . T y p e } ,   { " W i n g t i p   T o y s   ( H e a d   O f f i c e ) " ,   I n t 6 4 . T y p e } ,   { " G r a n d   T o t a l " ,   I n t 6 4 . T y p e } } ) , 
 
         # " R e m o v e d   C o l u m n s "   =   T a b l e . R e m o v e C o l u m n s ( # " C h a n g e d   T y p e 1 " , { " G r a n d   T o t a l " } ) , 
 
         # " U n p i v o t e d   O t h e r   C o l u m n s "   =   T a b l e . U n p i v o t O t h e r C o l u m n s ( # " R e m o v e d   C o l u m n s " ,   { " C o l u m n 1 " ,   " R o w   L a b e l s " } ,   " A t t r i b u t e " ,   " V a l u e " ) , 
 
         # " R e n a m e d   C o l u m n s "   =   T a b l e . R e n a m e C o l u m n s ( # " U n p i v o t e d   O t h e r   C o l u m n s " , { { " C o l u m n 1 " ,   " C a l e n d a r   Y e a r " } ,   { " R o w   L a b e l s " ,   " M o n t h " } ,   { " A t t r i b u t e " ,   " B i l l   T o   C u s t o m e r " } ,   { " V a l u e " ,   " T a r g e t " } } ) , 
 
         # " G r o u p e d   R o w s "   =   T a b l e . G r o u p ( # " R e n a m e d   C o l u m n s " ,   { " C a l e n d a r   Y e a r " ,   " B i l l   T o   C u s t o m e r " } ,   { { " T a r g e t " ,   e a c h   L i s t . S u m ( [ T a r g e t ] ) ,   t y p e   n u m b e r } } ) 
 
 i n 
 
         # " G r o u p e d   R o w s " 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 8  f A d d O n e   c u s t o m   f u n c t i o n 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 ( M y N u m b e r   a s   n u m b e r )   a s   n u m b e r   = > 
 
 l e t 
 
         S o u r c e   =   M y N u m b e r   +   1 
 
 i n 
 
         S o u r c e 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 9  F u l l   M   c o d e   o f   t h e   C h a n g i n g F o r m a t R e v i e w   q u e r y 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 l e t 
 
         S o u r c e   =   E x c e l . W o r k b o o k ( F i l e . C o n t e n t s ( " C : \ C o m p a n i o n \ C h a n g e F o r m a t . x l s x " ) ,   n u l l ,   t r u e ) , 
 
         S a l e s E x p o r t _ S h e e t   =   S o u r c e { [ I t e m = " S a l e s E x p o r t " , K i n d = " S h e e t " ] } [ D a t a ] , 
 
         # " F i l l e d   D o w n "   =   T a b l e . F i l l D o w n ( S a l e s E x p o r t _ S h e e t , { " C o l u m n 1 " ,   " C o l u m n 2 " } ) , 
 
         # " T r a n s p o s e d   T a b l e "   =   T a b l e . T r a n s p o s e ( # " F i l l e d   D o w n " ) , 
 
         # " F i l l e d   D o w n 1 "   =   T a b l e . F i l l D o w n ( # " T r a n s p o s e d   T a b l e " , { " C o l u m n 1 " ,   " C o l u m n 2 " ,   " C o l u m n 3 " } ) , 
 
         # " M e r g e d   C o l u m n s "   =   T a b l e . C o m b i n e C o l u m n s ( T a b l e . T r a n s f o r m C o l u m n T y p e s ( # " F i l l e d   D o w n 1 " ,   { { " C o l u m n 2 " ,   t y p e   t e x t } ,   { " C o l u m n 3 " ,   t y p e   t e x t } } ,   " e n - A U " ) , { " C o l u m n 1 " ,   " C o l u m n 2 " ,   " C o l u m n 3 " } , C o m b i n e r . C o m b i n e T e x t B y D e l i m i t e r ( " ^ " ,   Q u o t e S t y l e . N o n e ) , " M e r g e d " ) , 
 
         # " T r a n s p o s e d   T a b l e 1 "   =   T a b l e . T r a n s p o s e ( # " M e r g e d   C o l u m n s " ) , 
 
         # " P r o m o t e d   H e a d e r s "   =   T a b l e . P r o m o t e H e a d e r s ( # " T r a n s p o s e d   T a b l e 1 " ,   [ P r o m o t e A l l S c a l a r s = t r u e ] ) , 
 
         # " U n p i v o t e d   O t h e r   C o l u m n s "   =   T a b l e . U n p i v o t O t h e r C o l u m n s ( # " P r o m o t e d   H e a d e r s " ,   { " S a l e s   T e r r i t o r y ^ S a l e s   T e r r i t o r y ^ S a l e s   T e r r i t o r y " ,   " S t a t e   P r o v i n c e ^ S t a t e   P r o v i n c e ^ S t a t e   P r o v i n c e " } ,   " A t t r i b u t e " ,   " V a l u e " ) , 
 
         # " S p l i t   C o l u m n   b y   D e l i m i t e r "   =   T a b l e . S p l i t C o l u m n ( # " U n p i v o t e d   O t h e r   C o l u m n s " ,   " A t t r i b u t e " ,   S p l i t t e r . S p l i t T e x t B y D e l i m i t e r ( " ^ " ,   Q u o t e S t y l e . C s v ) ,   { " A t t r i b u t e . 1 " ,   " A t t r i b u t e . 2 " ,   " A t t r i b u t e . 3 " } ) , 
 
         # " P i v o t e d   C o l u m n "   =   T a b l e . P i v o t ( # " S p l i t   C o l u m n   b y   D e l i m i t e r " ,   L i s t . D i s t i n c t ( # " S p l i t   C o l u m n   b y   D e l i m i t e r " [ A t t r i b u t e . 1 ] ) ,   " A t t r i b u t e . 1 " ,   " V a l u e " ) , 
 
         # " R e p l a c e d   V a l u e "   =   T a b l e . R e p l a c e V a l u e ( # " P i v o t e d   C o l u m n " , " k " , " " , R e p l a c e r . R e p l a c e T e x t , { " S a l e s   A m o u n t " } ) , 
 
         # " M e r g e d   C o l u m n s 1 "   =   T a b l e . C o m b i n e C o l u m n s ( # " R e p l a c e d   V a l u e " , { " A t t r i b u t e . 3 " ,   " A t t r i b u t e . 2 " } , C o m b i n e r . C o m b i n e T e x t B y D e l i m i t e r ( " - " ,   Q u o t e S t y l e . N o n e ) , " D a t e " ) , 
 
         # " R e n a m e d   C o l u m n s "   =   T a b l e . R e n a m e C o l u m n s ( # " M e r g e d   C o l u m n s 1 " , { { " S a l e s   T e r r i t o r y ^ S a l e s   T e r r i t o r y ^ S a l e s   T e r r i t o r y " ,   " S a l e s   T e r r i t o r y " } ,   { " S t a t e   P r o v i n c e ^ S t a t e   P r o v i n c e ^ S t a t e   P r o v i n c e " ,   " S t a t e   P r o v i n c e " } } ) , 
 
         # " C h a n g e d   T y p e "   =   T a b l e . T r a n s f o r m C o l u m n T y p e s ( # " R e n a m e d   C o l u m n s " , { { " S a l e s   T e r r i t o r y " ,   t y p e   t e x t } ,   { " S t a t e   P r o v i n c e " ,   t y p e   t e x t } ,   { " D a t e " ,   t y p e   d a t e } ,   { " S a l e s   A m o u n t " ,   C u r r e n c y . T y p e } ,   { " T a x   R a t e " ,   P e r c e n t a g e . T y p e } } ) , 
 
         # " M u l t i p l i e d   C o l u m n "   =   T a b l e . T r a n s f o r m C o l u m n s ( # " C h a n g e d   T y p e " ,   { { " S a l e s   A m o u n t " ,   e a c h   _   *   1 0 0 0 ,   C u r r e n c y . T y p e } } ) , 
 
         # " R e o r d e r e d   C o l u m n s "   =   T a b l e . R e o r d e r C o l u m n s ( # " M u l t i p l i e d   C o l u m n " , { " D a t e " ,   " S a l e s   T e r r i t o r y " ,   " S t a t e   P r o v i n c e " ,   " S a l e s   A m o u n t " ,   " T a x   R a t e " } ) 
 
 i n 
 
         # " R e o r d e r e d   C o l u m n s " 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 1 0  D a t e   n a t i v e   q u e r y 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 s e l e c t   [ _ ] . [ D a t e ] , 
 
         [ _ ] . [ D a y   N u m b e r ] , 
 
         [ _ ] . [ D a y ] , 
 
         [ _ ] . [ M o n t h ] , 
 
         [ _ ] . [ S h o r t   M o n t h ] , 
 
         [ _ ] . [ C a l e n d a r   M o n t h   N u m b e r ] , 
 
         [ _ ] . [ C a l e n d a r   M o n t h   L a b e l ] , 
 
         [ _ ] . [ C a l e n d a r   Y e a r ] , 
 
         [ _ ] . [ C a l e n d a r   Y e a r   L a b e l ] , 
 
         [ _ ] . [ F i s c a l   M o n t h   N u m b e r ] , 
 
         [ _ ] . [ F i s c a l   M o n t h   L a b e l ] , 
 
         [ _ ] . [ F i s c a l   Y e a r ] , 
 
         [ _ ] . [ F i s c a l   Y e a r   L a b e l ] , 
 
         [ _ ] . [ I S O   W e e k   N u m b e r ] 
 
 f r o m   [ D i m e n s i o n ] . [ D a t e ]   a s   [ _ ] 
 
 w h e r e   [ _ ] . [ D a t e ]   > =   c o n v e r t ( d a t e t i m e 2 ,   ' 2 0 1 4 - 0 1 - 0 1   0 0 : 0 0 : 0 0 ' ) 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 1 1  S a l e   n a t i v e   q u e r y 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 s e l e c t   [ _ ] . [ C i t y   K e y ] , 
 
         [ _ ] . [ C u s t o m e r   K e y ] , 
 
         [ _ ] . [ B i l l   T o   C u s t o m e r   K e y ] , 
 
         [ _ ] . [ S t o c k   I t e m   K e y ] , 
 
         [ _ ] . [ I n v o i c e   D a t e   K e y ] , 
 
         [ _ ] . [ D e l i v e r y   D a t e   K e y ] , 
 
         [ _ ] . [ S a l e s p e r s o n   K e y ] , 
 
         [ _ ] . [ W W I   I n v o i c e   I D ] , 
 
         [ _ ] . [ Q u a n t i t y ] , 
 
         [ _ ] . [ U n i t   P r i c e ] , 
 
         [ _ ] . [ T a x   R a t e ] , 
 
         [ _ ] . [ T o t a l   E x c l u d i n g   T a x ] , 
 
         [ _ ] . [ T a x   A m o u n t ] , 
 
         [ _ ] . [ P r o f i t ] , 
 
         [ _ ] . [ T o t a l   I n c l u d i n g   T a x ] 
 
 f r o m   
 
 ( 
 
         s e l e c t   [ C i t y   K e y ] , 
 
                 [ C u s t o m e r   K e y ] , 
 
                 [ B i l l   T o   C u s t o m e r   K e y ] , 
 
                 [ S t o c k   I t e m   K e y ] , 
 
                 [ I n v o i c e   D a t e   K e y ] , 
 
                 [ D e l i v e r y   D a t e   K e y ] , 
 
                 [ S a l e s p e r s o n   K e y ] , 
 
                 [ W W I   I n v o i c e   I D ] , 
 
                 [ Q u a n t i t y ] , 
 
                 [ U n i t   P r i c e ] , 
 
                 [ T a x   R a t e ] , 
 
                 [ T o t a l   E x c l u d i n g   T a x ] , 
 
                 [ T a x   A m o u n t ] , 
 
                 [ P r o f i t ] , 
 
                 [ T o t a l   I n c l u d i n g   T a x ] 
 
         f r o m   [ F a c t ] . [ S a l e ]   a s   [ $ T a b l e ] 
 
 )   a s   [ _ ] 
 
 w h e r e   [ _ ] . [ I n v o i c e   D a t e   K e y ]   > =   c o n v e r t ( d a t e t i m e 2 ,   ' 2 0 1 4 - 0 1 - 0 1   0 0 : 0 0 : 0 0 ' ) 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 1 2  F r a g m e n t s   o f   D a t e   a n d   S a l e   q u e r i e s 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 / /   F r a g m e n t   o f   D a t e   q u e r y 
 
 s e l e c t   [ _ ] . [ D a t e ] , 
 
         .   .   . 
 
 f r o m   [ D i m e n s i o n ] . [ D a t e ]   a s   [ _ ] 
 
 w h e r e   [ _ ] . [ D a t e ]   > =   c o n v e r t ( d a t e t i m e 2 ,   ' 2 0 1 5 - 0 1 - 0 1   0 0 : 0 0 : 0 0 ' ) 
 
 / /   F r a g m e n t   o f   S a l e   q u e r y 
 
 s e l e c t   [ _ ] . [ C i t y   K e y ] , 
 
         .   .   . 
 
         f r o m   [ F a c t ] . [ S a l e ]   a s   [ $ T a b l e ] 
 
 )   a s   [ _ ] 
 
 w h e r e   [ _ ] . [ I n v o i c e   D a t e   K e y ]   > =   c o n v e r t ( d a t e t i m e 2 ,   ' 2 0 1 5 - 0 1 - 0 1   0 0 : 0 0 : 0 0 ' ) 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 1 3  D i s t i n c t C u s t o m e r   q u e r y   c o d e 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 l e t 
 
         S o u r c e   =   T a r g e t , 
 
         # " B i l l   T o   C u s t o m e r "   =   S o u r c e [ B i l l   T o   C u s t o m e r ] , 
 
         # " R e m o v e d   D u p l i c a t e s "   =   L i s t . D i s t i n c t ( # " B i l l   T o   C u s t o m e r " ) 
 
 i n 
 
         # " R e m o v e d   D u p l i c a t e s " 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 1 4  F i l t e r e d   C u s t o m e r   M   q u e r y 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 l e t 
 
         S o u r c e   =   S q l . D a t a b a s e s ( " l o c a l h o s t " ) , 
 
         W i d e W o r l d I m p o r t e r s D W   =   S o u r c e { [ N a m e = " W i d e W o r l d I m p o r t e r s D W " ] } [ D a t a ] , 
 
         D i m e n s i o n _ C u s t o m e r   =   W i d e W o r l d I m p o r t e r s D W { [ S c h e m a = " D i m e n s i o n " , I t e m = " C u s t o m e r " ] } [ D a t a ] , 
 
         # " F i l t e r e d   R o w s "   =   T a b l e . S e l e c t R o w s ( D i m e n s i o n _ C u s t o m e r ,   e a c h   [ B i l l   T o   C u s t o m e r ] 
 
   =   C u s t o m e r P a r a m e t e r ) 
 
 i n 
 
         # " F i l t e r e d   R o w s " 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 1 5  F i l t e r e d   C u s t o m e r   n a t i v e   q u e r y 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 s e l e c t   [ _ ] . [ C u s t o m e r   K e y ] , 
 
         [ _ ] . [ W W I   C u s t o m e r   I D ] , 
 
         [ _ ] . [ C u s t o m e r ] , 
 
         [ _ ] . [ B i l l   T o   C u s t o m e r ] , 
 
         [ _ ] . [ C a t e g o r y ] , 
 
         [ _ ] . [ B u y i n g   G r o u p ] , 
 
         [ _ ] . [ P r i m a r y   C o n t a c t ] , 
 
         [ _ ] . [ P o s t a l   C o d e ] , 
 
         [ _ ] . [ V a l i d   F r o m ] , 
 
         [ _ ] . [ V a l i d   T o ] , 
 
         [ _ ] . [ L i n e a g e   K e y ] 
 
 f r o m   [ D i m e n s i o n ] . [ C u s t o m e r ]   a s   [ _ ] 
 
 w h e r e   [ _ ] . [ B i l l   T o   C u s t o m e r ]   =   ' N / A ' 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 1 6  C u s t o m   c o l u m n   f o r m u l a   t o   r e p l a c e   n u l l s   w i t h   p r e - c a l c u l a t e d   a v e r a g e s 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 = i f   [ A v e r a g e   P r i c e ]   i s   n u l l   t h e n   # " A v e r a g e   P r i c e "   e l s e   [ A v e r a g e   P r i c e ] 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 1 7  C u s t o m   c o l u m n   f o r m u l a   t h a t   h a n d l e s   e r r o r s 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 = t r y   [ C a l e n d a r Y e a r ]   o t h e r w i s e   n u l l 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 L I S T I N G   1 - 1 8  F r a g m e n t   o f   a   q u e r y   w i t h   T r i m   t r a n s l a t e d   t o   S Q L 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 s e l e c t   [ _ ] . [ E m p l o y e e   K e y ]   a s   [ E m p l o y e e   K e y ] , 
 
         [ _ ] . [ W W I   E m p l o y e e   I D ]   a s   [ W W I   E m p l o y e e   I D ] , 
 
         l t r i m ( r t r i m ( [ _ ] . [ E m p l o y e e ] ) )   a s   [ E m p l o y e e ] , 
 
         [ _ ] . [ P r e f e r r e d   N a m e ]   a s   [ P r e f e r r e d   N a m e ] , 
 
         [ _ ] . [ I s   S a l e s p e r s o n ]   a s   [ I s   S a l e s p e r s o n ] , 
 
         [ _ ] . [ P h o t o ]   a s   [ P h o t o ] , 
 
         [ _ ] . [ V a l i d   F r o m ]   a s   [ V a l i d   F r o m ] , 
 
         [ _ ] . [ V a l i d   T o ]   a s   [ V a l i d   T o ] , 
 
         [ _ ] . [ L i n e a g e   K e y ]   a s   [ L i n e a g e   K e y ] 
 
 f r o m   [ D i m e n s i o n ] . [ E m p l o y e e ]   a s   [ _ ] 